const net = require("net");

const server = net.createServer((socket) => {
  socket.on("data", (data) => {
    console.log(`Received: ${data}`);
    socket.write(`Hello from server!\n`);
  });

  socket.on("end", () => {
    console.log("Client disconnected");
  });

  socket.on("error", (err) => {
    console.error(`Error: ${err}`);
  });

  // 设置定时器，每隔一段时间发送消息给客户端  
  setInterval(() => {  
    const message = '这是定时发送的消息\n';  
    socket.write(message);  
  }, 1000); // 每5秒发送一次消息  
});

server.on("error", (err) => {
  throw err;
});

server.listen(57678, () => {
  console.log("Server listening on port 8124");
});

server.client
